package 剑指offer_力扣版.Month07.day04数组;

public class 二维子矩阵的和 {
    int[][] dp;
    public 二维子矩阵的和(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; i++) {
            int res = 0;
            for (int j = 0; j < n; j++) {
                res += matrix[i][j];
                dp[i + 1][j + 1] = dp[i][j + 1] + res;
            }
        }

    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return dp[row2 + 1][col2 + 1] - dp[row2 + 1][col1] - dp[row1][col2 + 1] + dp[row1][col1];
    }
}
